package editor.graphics;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;

import editor.modelo.EditorFloor;
import editor.modelo.LandMark;
import editor.modelo.Portal;
import editor.modelo.EditorPortal;
import editor.modelo.Space;
import editor.modelo.EditorSpace;

public class Seleccion {

	//items seleccionados, null es no seleccionado
	private ArrayList<Space> spaceSelec = new  ArrayList<Space>();
	//	private Edge edgeSelec;
	private ArrayList<Point2D> pointSelec = new ArrayList<Point2D>(); //Se usa de auxiliar para agregarEdges para recordar el ultimo punto

	private  Portal portalSelec;
	private LandMark landSelec;

	public void vaciarPuntos(){
		//spaceSelec = null;
		//	edgeSelec = null;
		pointSelec.clear();
	}
	public void vaciarSpaces(){
		spaceSelec.clear();
	}

	public void vaciarPortals() {
		portalSelec = null;
	}
	
	public void vaciarLandMarks(){
		landSelec = null;
	}
	
	public void vaciar() {
		vaciarPortals();
		vaciarPuntos();
		vaciarSpaces();
		vaciarLandMarks();
	}
	
	public boolean isEmpty(){
		return !(isSpaceSelected()|| isPointSelected() || isPortalSelected() || isLandMarkSelected() );
	}

	/**
	 * Seleccionar un espacio des-selecciona todo el resto
	 * @param s
	 */
	public void selectSpace(Space s){
	//	System.out.println("select" + s);
	//	if(!isSelected(s)){
			vaciar();
			//spaceSelec.clear();	
			spaceSelec.add(s);
	//	}
	//	else{
	//		spaceSelec.clear();
		
	//	}
	}
	/**
	 * Al agregar un espacio ya agregado se elimina junto a sus
	 * puntos seleccionados
	 * @param s
	 */
	public void addSpace(Space s) {
		if(isSelected(s)){
			spaceSelec.remove(s);
			for(Point2D pto: s.getBorde()){
				if(isSelected(pto)){
					pointSelec.remove(pto);
				}
			}
			return;
		}
		spaceSelec.add(s);

	}

	public Space getLastSelectedSpace(){
		if(isSpaceSelected())
			return spaceSelec.get(spaceSelec.size()-1);
		else
			return null;
	}

	public List<Space> getSelectedSpaces(){
		return spaceSelec;
	}
	
	public boolean isSpaceSelected(){
		return spaceSelec.size()>0;
	}

	/**
	 * Seleccionar un punto des-selecciona todo menos a los spaces
	 * @param p
	 */
	public void selectPoint(Point2D p){
	//	if(!isSelected(p)){
			pointSelec.clear();
			portalSelec = null;
			addPoint(p);
	//	}
	//	else{
	//		pointSelec.clear();
	//	}
	}

	public void addPoint(Point2D p) {
		if(isSelected(p)){
			pointSelec.remove(p);
			return;
		}
		pointSelec.add(p);

	}

	public Point2D getLastSelectedPoint(){
		if(pointSelec.isEmpty()) return null;
		return pointSelec.get(pointSelec.size()-1);
	}

	public List<Point2D> getSelectedPoints(){
		return pointSelec;
	}

	public boolean isPointSelected(){
		return pointSelec.size()>0;
	}
	/*
	 * Seleccionar un portal des-selecciona todo el resto
	 */
	public void selectPortal(Portal p){
		this.vaciar();
		this.portalSelec = p;
	}
	public boolean isPortalSelected(){
		return portalSelec != null;
	}
	public Portal getLastSelectedPortal(){
		return portalSelec;
	}
	
	//Landmarks///////////////////////////////////////
	public void selectLandMark(LandMark lm) {
		landSelec =lm;
		
	}
	public boolean isLandMarkSelected(){
		return landSelec != null;
	}


	public boolean isSelected(Object o){
		if(o instanceof Space) return spaceSelec.contains(o);
		//	if(o instanceof Edge) return o.equals(edgeSelec);
		if(o instanceof Point2D){
			return pointSelec.contains(o);
		} 
		if(o instanceof Portal){
			return o.equals(portalSelec);
		}
		if(o instanceof LandMark){
			return o.equals(landSelec);
		}
		return false;
	}
	

	public EditorSpace seleccionarSpace(EditorFloor piso, Point2D.Float pt) {
		for(Space s: piso.getEspacios()){
			if(s.getCentro().distance(pt)< 8 ){
				return (EditorSpace) s;
			}
		}
		return null;
	}
	
	public LandMark seleccionarLandmark(EditorFloor piso, Point2D.Float pt) {
		for(LandMark l: piso.getLandmarks()){
			if(l.getCentro().distance(pt)< 8 ){
				return (LandMark) l;
			}
		}
		return null;
	}
	public Point2D seleccionarPunto(Point2D.Float pt){
		for(Space s: this.getSelectedSpaces())
			for(Point2D p: s.getBorde()){
				if(p.distance(pt)<5){
					return p;
				}
			}
		return null;
	}
	public Portal seleccionarPortal(EditorFloor piso, Point2D.Float pt) {
		for(Portal por: piso.getPortales()){
			if(((EditorPortal)por).containsPoint(pt)){
				return por;
			}
		}
		return null;
	}
	


}
